package de.mouseless.csv2sql.model;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import de.mouseless.csv2sql.common.ApplicationException;

public class DDLModel {
   Set<String> nsTablesEtc = new HashSet<String>(); 
   Map<String, Table> tables = new HashMap<String, Table>();
   Map<String, AbstractConstraint> constraints = new HashMap<String, AbstractConstraint>(); 
   Map<String, Index> indices = new HashMap<String, Index>(); 
   
   public void addTable(Table table) {
      if(tables.containsKey(table.getName())) {
         throw new ApplicationException("Table name " + table.getName() + "alreay exists");
      }
      tables.put(table.getName(), table);
      nsTablesEtc.add(table.getName());
   }
   
   public void addConstraint(AbstractConstraint cons) {
      String name = cons.getName();
      int i=1;
      while(constraints.containsKey(name)) {
         name = cons.getName()+i;
         i++;
      }
      cons.setName(name);
      if(name.length()>30) {
         throw new ApplicationException("Name to long for constraint " + name);
      }
      constraints.put(name, cons);
   }

   public void addIndex(Index idx) {
      String name = idx.getName();
      int i=1;
      while(indices.containsKey(name)) {
         name = idx.getName()+i;
         i++;
      }
      idx.setName(name);
      if(name.length()>30) {
         throw new ApplicationException("Name to long for index " + name);
      }
      indices.put(name, idx);
   }
}
